package cn.edu.nju.software.model.administrator;

import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.nju.software.model.DatabaseHelper;

public class AdministratorLoginVerifier {

	private static DatabaseHelper dbHelper;

	public AdministratorLoginVerifier() {
		if (dbHelper == null) {
			dbHelper = new DatabaseHelper();
		}
	}

	/**
	 * Verify the login of the administrator
	 * 
	 * @param Username
	 *            username entered by the user
	 * @param Password
	 *            password entered by the user
	 * @return Return true if the username and password pair is legal, false
	 *         otherwise.
	 */
	public boolean verify(String username, String password) {
		System.out.println("Entered: " + username + "; " + password);
		boolean result = false;

		ResultSet rs = dbHelper.fetchAllRecord(DatabaseHelper.TID_ADMIN);
		if (rs != null) {
			try {
				rs.beforeFirst();
				while (rs.next()) {
					String u = rs.getString(2);
					String p = rs.getString(3);
					System.out.println("Checked: " + u + "; " + p);
					if (username.equals(u) && password.equals(p)) {
						System.out.println("bingo login");
						result = true;
						break;
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		return result;
	}
}
